********************************************************************************
* PRODUCES CYCLICAL CONCENTRATION  MEASURES AND STEADY STATE CONCENTRATION *****
********************************************************************************
* INPUT:  ${orig}/${concentrationfile}_new.dta	: s.a. concentration series 
* OUTPUT: qoq_concentration_72_sa_${version}.dta and citydata_72${version}.dta

use ${orig}/${concentrationfile}_new.dta, clear

forv n = 1(1)$N {
replace herfindahl`n' = herfindahl`n'^.5
replace herfindahl_sa`n' = herfindahl_sa`n'^.5
}

forv n = 1(1)$N {
qui: gen log${var_x}`n' = ln(${var_x}`n')	
* INSPECT ANY BREAKS IN THE SERIES:
preserve
qui: keep log${var_x}`n' eventdate idtime
qui: reg log${var_x}`n' idtime
qui: tsset eventdate
qui: estat sbsingle
gen estimatedbreakdate`n'="`r(breakdate)'"
if `n'==1{
qui: keep  estimatedbreakdate`n'
qui: gen aux=1
qui: duplicates drop 
qui: save breakanalysisx.dta, replace
}
else{
qui: keep  estimatedbreakdate`n'
qui: duplicates drop 
qui: gen aux=1
qui: merge 1:1 aux using breakanalysisx.dta
qui: drop _merge
qui: save breakanalysisx.dta, replace
}
restore
}

preserve
use breakanalysisx.dta, clear
reshape long estimatedbreakdate, i(aux) j(amr)
tab estimatedbreakdate
restore

forv n = 1(1)$N {
qui: tsfilter hp cycle_hp_${var_x}`n' = log${var_x}`n', smooth(1600)
qui: reg log${var_x}`n' idtime
qui: mat A = e(b)
qui: gen cycle_linear_${var_x}`n' = log${var_x}`n' - A[1,1]*idtime - A[1,2]
qui: gen constant_linear_${var_x}`n' = A[1,2]
}

qui: keep eventdate cycle_linear_${var_x}* cycle_hp_${var_x}* constant_linear_${var_x}* log${var_x}*
qui: reshape long cycle_linear_${var_x} cycle_hp_${var_x} constant_linear_${var_x} log${var_x}, i(eventdate) j(amr)
qui: egen idtime = group(eventdate)
qui: save qoq_concentration_72_sa_${version}.dta, replace

qui: keep amr constant_linear_${var_x} 
qui: duplicates drop
qui: merge 1:1 amr using ranking_72.dta
qui: drop if _merge!=3
qui: drop _merge
qui: gen m_${var_x}_wgt = constant_linear_${var_x}
qui: keep m_* amr  
if "${var_x}" == "herfindahl"{
gen measure = "herfindahl"
}
else{
gen measure = "top employment share"
}
save citydata_72${version}.dta, replace
********************************************************************************
* TREND OVER TIME OF CONCENTRATION MEASURE *************************************
********************************************************************************
local listcontrol "1 3"
foreach cc of local listcontrol{
global version = `cc'
do ${do}/settingglobals.do

use qoq_concentration_72_sa_${version}.dta, clear
qui: keep log* cycle_linear* eventdate amr
qui: egen idtime = group(eventdate)
replace log${var_x}=exp(log${var_x})
qui: gen m = .
qui: gen u = .
qui: gen l = .
qui: sum idtime
forv t=1(1)`r(max)'{
qui: sum log* if idtime == `t', detail
qui: replace m = `r(p50)' if idtime == `t'
qui: sum log* if idtime == `t', detail
qui: replace u = `r(p90)' if idtime == `t'
qui: sum log* if idtime == `t', detail
qui: replace l = `r(p10)' if idtime == `t'
}
qui: keep m u l eventdate
qui: duplicates drop
twoway (tsline m, lcolor(black)) (tsline u, lpattern(dash) lcolor(black)) (tsline l, lpattern(dash) lcolor(black)), graphregion(color(white)) xtitle("Time") ytitle("Concentration") xtitle("") legend(label(1 "Median") label(2 "90th pct") label(3 "10th pct"))
graph export ${graphs}/concentrationdecline${var_x}.png, replace
}
********************************************************************************


